package day_2022_9_to_12.Day;

/**
 * @author haomin
 * @date 2022/11/05 23:57
 **/
public class Day63_1_Num1106_ParseBoolExpr {
    class Solution {
        public boolean parseBoolExpr(String ex) {
            if(ex.equals("t")){return true;}
            if(ex.equals("f")){return false;}
            char start=ex.charAt(0);
            int n=ex.length();
            if(start=='!'){return !parseBoolExpr(ex.substring(2,n-1));}
            ex=ex.substring(2,n-1)+",";
            for(int i=0,j=0,count=0;i<n-2;i++){
                char ch=ex.charAt(i);
                if(ch=='('){count++;}
                else if(ch==')'){count--;}
                else if(ch==','&&count==0){
                    boolean b=parseBoolExpr(ex.substring(j,i));
                    if(start=='|'&&b){return true;}
                    if(start=='&'&&!b){return false;}
                    j=i+1;
                }
            }
            return start=='&';
        }
    }
}